import {useSettingsStore} from "@/data/stores/settings";
import {JobConstants} from '@/data/constants/Constants';
import moment from "moment";
import type {Log} from "@/data/entity/Log";
import {jobs} from "@/data/constants/Jobs";

const settingsStore = useSettingsStore()
let jobMap = new Map

export function formatLogListToExcel(_logList:Log[]):any[]{
    let formattedLogList = []

    _logList = JSON.parse(JSON.stringify(_logList))

    _logList.sort((a, b) => {
        return a.count > b.count ? 1:-1
    })

    for (let log of _logList){
        let date = new Date(log.date)
        let formattedLog = {
            [settingsStore.settings.excelKeySetting.count]: log.count || -1,
            [settingsStore.settings.excelKeySetting.duty]: log.duty || '',
            [settingsStore.settings.excelKeySetting.job]: jobs.get(log.job)!.name || jobs.get(JobConstants.NONE)!.name,
            [settingsStore.settings.excelKeySetting.completed]: log.completed? '完成':'中途退出',
            [settingsStore.settings.excelKeySetting.description]: log.description || '',
            [settingsStore.settings.excelKeySetting.image]: log.image,
            [settingsStore.settings.excelKeySetting.date]: moment(date).format('YYYY/MM/DD'),
            [settingsStore.settings.excelKeySetting.time]: moment(date).format('HH:mm:ss')
        }
        formattedLogList.push(formattedLog)
    }

    return formattedLogList
}

export function formatLogListFromExcel(_logList:any[]):Log[] {
    let logList:Log[] = []

    for (let item of settingsStore.settings.logFormatSetting.jobMap){
        jobMap.set(item[1], item[0])
    }

    _logList.forEach(item=>{
        let log:Log = formatLogFromExcel(item)
        logList.push(log)
    })
    return logList
}


export function formatLogFromExcel(_log:any):Log {
    return {
        'count': _log[settingsStore.settings.excelKeySetting.count] || -1,
        'duty': _log[settingsStore.settings.excelKeySetting.duty] || '', // 副本
        'job': jobFormatter(_log[settingsStore.settings.excelKeySetting.job]), // 职业
        'completed': completedFormatter(_log[settingsStore.settings.excelKeySetting.completed]), // 完成情况
        'description': _log[settingsStore.settings.excelKeySetting.description] || '', // 描述
        'image': _log[settingsStore.settings.excelKeySetting.image] || '',
        'date': dateFormatter(_log[settingsStore.settings.excelKeySetting.date], _log[settingsStore.settings.excelKeySetting.time]) // 日期
    }
}

function jobFormatter(job:string){
    return jobMap.get(job) || JobConstants.UNKNOWN
}

function completedFormatter(_completed:any){
    if (_completed === undefined){
        return true
    }
    return _completed == settingsStore.settings.logFormatSetting.completed
}

function dateFormatter(date:string|number|undefined, time:string|number|undefined):Date{
    let resultDate = new Date(0)
    if (date === undefined){
        return resultDate
    }
    let offset = (new Date()).getTimezoneOffset() * 60 * 1000

    if ((typeof date) === 'string') {
        resultDate = new Date(date)
    }else if ((typeof date) === 'number') {
        resultDate = new Date( (<number>date - 25569) * 24 * 3600000 + offset)
    }

    if (time !== undefined && time != null && time !== '') {
        if ((typeof time) === 'string') {
            resultDate = new Date(resultDate.getFullYear() + '/'
                + (1 + resultDate.getMonth()) + '/'
                + resultDate.getDate() + ' '
                + time)
        }else if ((typeof time) === 'number') {
            resultDate = new Date( resultDate.getTime() + <number>time * 24 * 3600000)
        }
    }


    return resultDate
}